package com.sxran.im.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.dao.ImNotificationMapper;
import com.sxran.im.info.ImNotification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class NotificationService {
    @Resource
    private ImNotificationMapper imNotificationMapper;
    public ImNotification updateOrInsert(ImNotification bean){
        LambdaQueryWrapper<ImNotification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImNotification::getUuid,bean.getUuid());
        queryWrapper.eq(ImNotification::getToId,bean.getToId());

        ImNotification imFriendInvite = imNotificationMapper.selectOne(queryWrapper);

        if (imFriendInvite == null) {
            imFriendInvite = bean;
            imNotificationMapper.insert(imFriendInvite);
        }else{
            bean.setState(0);
            imNotificationMapper.update(bean,queryWrapper);
        }
        return imFriendInvite;
    }
    public int updateFriendApplicationState(String fromUid , String toId , Integer state ,String content ){
        return this.updateApplicationState(fromUid,toId,state,29,content);
    }
    public int updateGroupApplicationState(String fromUid , String toId , Integer state ,String content ){
        return this.updateApplicationState(fromUid,toId,state,36,content);
    }
    public int updateApplicationState(String fromUid , String toId , Integer state ,Integer type,String content ){
        ImNotification bean = new ImNotification();
        bean.setState(state);
        bean.setContent(content);
        bean.setIsRead(1);
        // 匹配条件
        LambdaQueryWrapper<ImNotification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( ImNotification::getFromUid, fromUid);
        queryWrapper.eq( ImNotification::getToId, toId);
        queryWrapper.eq( ImNotification::getType, type );

        return imNotificationMapper.update(bean,queryWrapper);
    }
}
